package com.fick.tags;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.BaseHibernateDAO;
import com.fick.hibernate.dao.LinkDAO;
import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.IsGray;
import com.fick.hibernate.model.Link;
import com.fick.hibernate.model.News;
import com.fick.util.General;
/**
 * 显示指定链接信息
 * @author 郭成意
 * 2009-08-14
 * 
 * **/
public class LinkClassTag extends TagSupport {
	private static Log log = LogFactory.getLog(LinkClassTag.class);
	//链接类型id
	private int classid=0;
	//从第几条开始
	private int beginNum=1;
	//显示几条
	private int num=10;
	//table属性
	private String tableAttr="";
	//tr属性
	private String trAttr="";
	//title td 属性
	private String tdAttr="";
	//<a>标签属性
	private String linkAttr="";
	
	private String dotAttr="";
	
	private String  dotSrc;
	private String isVertical="true";
	
	public int doStartTag() throws JspException {
			
		try {
			StringBuffer sb = new StringBuffer();
			NewsDAO dao=new NewsDAO();
			LinkDAO linkdao=new LinkDAO();
			List<Link> list=dao.findPageBySql(Link.class,1,num,"where linkClass.classid="+classid+" order by linkOrder");
			//List<News> list=dao.findPageByHql("where isAss='1' and channelid.channelId="+channelid+" order by addDate desc", 1, beginNum+num);
			if(list!=null)
			{
				if(isVertical.equals("true"))
				{
					sb.append("<table ");
					sb.append(tableAttr);
					sb.append("><tbody>");
					for(int i=beginNum-1;i<list.size();i++)
					{
						Link link=list.get(i);
						sb.append("<tr ");
						sb.append(trAttr);
						sb.append(">");
						if(dotAttr.trim().length()>0)
						{
							sb.append("<td ");
							sb.append(dotAttr);
							sb.append("><img src=\""+dotSrc+"\">");
							sb.append("</td>");
						}
						sb.append("<td ");
						sb.append(tdAttr);
						sb.append(">");
						sb.append("<a href="+link.getWebUrl()+" ");
						sb.append(linkAttr);
						sb.append(">");
						if(link.getImg()!=null && !link.getImg().equals(""))
						{
							BaseHibernateDAO daoGray = new BaseHibernateDAO();
						    List grayList = daoGray.findBySql(IsGray.class,"");
						    if(grayList!=null&&grayList.size()>0){
						    	IsGray is = (IsGray)grayList.get(0);
						    	if("2".equals(is.getIsGray())){
						    		sb.append("<img src=\"/"+"redLinkPic/"+link.getImg()+"\" border=\"0\">");
						    	} else {
									sb.append("<img src=\"/"+link.getImg()+"\" border=\"0\">");						    		
						    	}
						    }
						}else{
							sb.append(link.getWebName());
						}
						sb.append("</a>");
						sb.append("</td>");
						sb.append("</tr>");
						
					}
					sb.append("</tbody>");
					sb.append("</table>");	
				}else
				{
					sb.append("<tr ");
					sb.append(trAttr);
					sb.append(">");
					for(int i=beginNum-1;i<list.size();i++)
					{
						Link link=list.get(i);
						if(dotAttr.trim().length()>0)
						{
							sb.append("<td ");
							sb.append(dotAttr);
							sb.append("><img src=\""+dotSrc+"\">");
							sb.append("</td>");
						}
						sb.append("<td ");
						sb.append(tdAttr);
						sb.append(">");
						sb.append("<a href="+link.getWebUrl()+" ");
						sb.append(linkAttr);
						sb.append(">");
						if(link.getImg()!=null && !link.getImg().equals(""))
						{
							sb.append("<img src=\"/"+link.getImg()+"\" border=\"0\">");
						}else{
							sb.append(link.getWebName());
						}
						sb.append("</a>");
						sb.append("</td>");
					
						
					}
					sb.append("</tr>");
				}
			}
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		}catch(IOException e)
		{
			log.error("标签显示错误"+e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	public int getBeginNum() {
		return beginNum;
	}
	public void setBeginNum(int beginNum) {
		this.beginNum = beginNum;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getTableAttr() {
		return tableAttr;
	}
	public void setTableAttr(String tableAttr) {
		this.tableAttr = tableAttr;
	}
	public String getTrAttr() {
		return trAttr;
	}
	public void setTrAttr(String trAttr) {
		this.trAttr = trAttr;
	}
	public String getLinkAttr() {
		return linkAttr;
	}
	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}
	public int getClassid() {
		return classid;
	}
	public void setClassid(int classid) {
		this.classid = classid;
	}
	public String getTdAttr() {
		return tdAttr;
	}
	public void setTdAttr(String tdAttr) {
		this.tdAttr = tdAttr;
	}
	public String getDotAttr() {
		return dotAttr;
	}
	public void setDotAttr(String dotAttr) {
		this.dotAttr = dotAttr;
	}
	public String getDotSrc() {
		return dotSrc;
	}
	public void setDotSrc(String dotSrc) {
		this.dotSrc = dotSrc;
	}
	public String getIsVertical() {
		return isVertical;
	}
	public void setIsVertical(String isVertical) {
		this.isVertical = isVertical;
	}
	
}
